import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			//数据
			total: 0, //总条数
			page: 1, //当前页
			size: 10, //每页显示条数
      search: '',
      expertSearch: '', //专家搜索信息
      notApplyExpertTotal: 0, //非申请专家总条数
      notApplyExpertPage: 1, //非申请专家当前页
      notApplyExpertSize: 5, //非申请专家每页显示条数
      applyExpertTotal: 0, //申请专家总条数
      applyExpertPage: 1, //申请专家当前页
      applyExpertSize: 10, //申请专家每页显示条数
      expertSearchType: 'expertName', //专家搜索类型（默认专家名称）
			demandModelShow: false,
			reviewTable: true,
			score: false,
			paperWorkBox: false,
			pingJiaZhiBox: false,
			Specialist: false,
			Tickets: false,
      projectDetailsModelShow: false, //项目详情模态框显示状态
      //新增评审相关对话框状态
      selectReviewMethodDialogFlag: false,
      internalReviewDialogFlag: false,
      organizationReviewDialogFlag: false,
      organizationReviewNextDialogFlag: false,
      //业务数据
			uploadNum: 0,
      reviewValidate: {}, //项目评审校验对象
      projectInfo: {},
      reviewExpertList: [], //评审专家
      notApplyExpertList: [], //非申请专家列表
      applyExpertList: [], //申请专家列表
      //projectApplicationList: [], //申请项目分页数据
      projectList: [], //项目分页数据
      projectReviewResultList: [], //项目评审结果信息
      projectReviewResultList2: [], //第二次项目评审结果信息
      reviewerExpertTemp: {
        reviewMethod: 0,//默认为内部评审
        type: 0, //评审方式打分制
      }, //临时评审专家
			FileUpload: [],
			curIndex:-1,//选中下标
		}
	},
	components: {
		pageination
	},
	created() { //调用方法
    this.searchTodo();
	},
	mounted() {
    let that = this;
    //加载评审表单验证
    that.reviewValidate = $("#reviewForm").validate({
      ignore: [],
      rules: {
        standardUrl: { // 评审标准地址
          required: true
        },
        endTime: { // 评审截止时间
          required: true
        },
        type: { // 评审类型
          required: true
        }
      },
      messages: {
        standardUrl: { // 评审标准地址
          required: "请上传评审标准"
        },
        endTime: { // 评审截止时间
          required: "请选择评审截止时间"
        },
        type: { // 评审类型
          required: "请选择评审类型"
        }
      },
      /*errorElement: "em",
      errorPlacement: function (error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });
  },
	methods: { //写函数方法
		//给选中的列加背景色
		choosed:function(index){
			this.curIndex=index;
		},
		//页码切换执行方法
		pageFn: function(val, pid) {
			this.page = parseInt(val);
			//此处调列表数据接口
      this.searchTodo();
		},
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},

    //页码切换执行方法
    notApplyExpertPageFn: function(val, pid) {
      this.notApplyExpertPage = parseInt(val);
      //此处调列表数据接口
      this.searchAllNotApplicationExpert(this.projectInfo.id);
    },
    //页码切换执行方法
    applyExpertPageFn: function(val, pid) {
      this.applyExpertPage = parseInt(val);
      //此处调列表数据接口
      this.searchAllApplyExpert(this.projectInfo.id);
    },
		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			}
		},
		/**文件上传js**/
		idFrontImg: function(event) {
      var that = this;
      var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
      let formData = new FormData();
      formData.append('fileNames', file);
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
      .then(function(res) {
        if(res.success) {
          let fileInfo = res.data[0];
          that.FileUpload = [that.getFileName(fileInfo.url)];
          that.reviewerExpertTemp.standardUrl = fileInfo.url;
          if(fileInfo.url){
          	$("#standardUrl-error").css("display","none");
          }else{
          	$("#standardUrl-error").css("display","block");
          }
          console.log("-2--"+that.reviewExpertList);
        }
      })
		},
		//评审组织模态框
		demand: function(id) {
      /*//重置申请专家分页信息
			this.applyExpertTotal = 0; //申请专家总条数
      this.applyExpertPage = 1; //申请专家当前页
      this.applyExpertSize = 10; //申请专家每页显示条数

      this.reviewExpertList = [];
      this.FileUpload = [];
      this.reviewerExpertTemp = {
        reviewMethod: 0,//默认为内部评审
        type: 0, //评审方式打分制
      };
      this.score = false;
      this.searchProject(id);
      this.searchAllApplyExpert(id);
      this.searchAllNotApplicationExpert(id);
      this.demandModelShow = true;*/
      this.reviewerExpertTemp = {
        reviewMethod: 0,//默认为内部评审
        type: 0, //评审方式打分制
      };
      this.searchProject(id);
      this.selectReviewMethodDialogFlag = true;
		},
    demand2: function(id) {
      /*//重置申请专家分页信息
			this.applyExpertTotal = 0; //申请专家总条数
      this.applyExpertPage = 1; //申请专家当前页
      this.applyExpertSize = 10; //申请专家每页显示条数

      this.reviewExpertList = [];
      this.FileUpload = [];
      this.reviewerExpertTemp = {
        reviewMethod: 0,//默认为内部评审
        type: 0, //评审方式打分制
      };
      this.score = false;
      this.searchProject(id);
      this.searchAllApplyExpert(id);
      this.searchAllNotApplicationExpert(id);
      this.demandModelShow = true;*/
      this.reviewerExpertTemp = {
        reviewMethod: 0,//默认为内部评审
        type: 0, //评审方式打分制
      };
      this.searchProject(id);
    },
		//关闭模态框
		closeModel: function() {
      $(".secondLevelPageination .jump_page>input").val("");
		  this.Specialist = false;
			this.demandModelShow = false;
			this.Tickets = false;
      this.projectDetailsModelShow = false;
      //新加项目评审对话框显示状态
      this.selectReviewMethodDialogFlag = false;
      this.internalReviewDialogFlag = false;
      this.organizationReviewDialogFlag = false;
      this.organizationReviewNextDialogFlag = false;
//
//			this.WorkModelShow = false;
		},
		//关闭内部评审对话框
		closeInternalReviewDialog:function(){
			this.internalReviewDialogFlag = false;
		},

		//关闭组织评审对话框
		closeOrganizationReviewDialog:function(){
			this.organizationReviewDialogFlag = false;
		},
		//关闭组织评审下一步对话框
		closeOrganizationReviewNextDialogFlag:function(){
			this.organizationReviewNextDialogFlag = false;
		},
		//组织评审下一步
		nextStep: function() {
			if (this.reviewExpertList == null || this.reviewExpertList.length == 0) {
        layer.msg("请选择评审专家！", {
          time: 3000, //3s后自动关闭
        });
			  return;
      }
      //清空表单数据
      $("input[type='reset']").trigger("click");
      $("#reviewForm").validate().resetForm();
		  this.reviewTable = false;

      //重置申请专家分页信息
      this.applyExpertTotal = 0; //申请专家总条数
      this.applyExpertPage = 1; //申请专家当前页
      this.applyExpertSize = 5; //申请专家每页显示条数
      this.score = true;
		},
		//评审专家模态框
		SpecialistBtn: function(id) {
      //重置申请专家分页信息
      this.applyExpertTotal = 0; //申请专家总条数
      this.applyExpertPage = 1; //申请专家当前页
      this.applyExpertSize = 10; //申请专家每页显示条数
      this.projectInfo.id = id;
      this.searchProject(id);
      this.searchAllApplyExpert(id);
      this.Specialist = true;
		},
		//评审结果模态框
		AllTickets: function(projectId) {
			this.Tickets = true;
      this.searchProjectReviewResult(projectId);
		},
    //显示项目详情模态框
    showProjectDetailsModel: function(projectId) {
		  this.projectDetailsModelShow = true;
		  this.searchProject(projectId);
    },
		//打分制
		paperWork: function() {
			this.paperWorkBox = true;
			this.pingJiaZhiBox = false;
		},
		//评价制
		pingJiaZhi: function() {
			this.pingJiaZhiBox = true;
			this.paperWorkBox = false;
		},
    //重新评价
    reappraisal: function(projectId) {
		  this.Tickets = false;
		  this.demand2(projectId);

      this.organizationReviewDialogFlag = false;
      //清空表单数据
      $("input[type='reset']").trigger("click");
      $("#reviewForm").validate().resetForm();
      this.FileUpload = [];
      this.applyExpertList = [];
      this.applyExpertTotal =  0; //申请专家总条数
      this.applyExpertPage = 1; //申请专家当前页
      this.applyExpertSize = 5; //申请专家每页显示条数
      this.projectInfo.id = projectId;
      this.searchAllReviewExpert(projectId,0,1);
      this.searchAllApplyExpert(projectId);
      this.organizationReviewNextDialogFlag = true;
    },
    //更换专家
    replaceExpert: function(projectId) {
      this.Tickets = false;
      this.demand2(projectId);

      this.selectReviewMethodDialog = false;
      //清空评审专家筛选
      this.notApplyExpertList = [];
      this.reviewExpertList = [];
      this.notApplyExpertTotal = 0; //非申请专家总条数
      this.notApplyExpertPage = 1; //非申请专家当前页
      this.notApplyExpertSize = 5; //非申请专家每页显示条数
      this.expertSearchType = 'expertName';
      this.expertSearch = '';
      this.searchAllNotApplicationExpert(projectId);
      this.organizationReviewDialogFlag = true;
    },
    //预览
    preview: function(url,msg) {
      if (url == null || url == "") {
        if (msg == null || msg == "") {
          msg = "附件";
        }
        layer.msg("暂无" + msg + "！");
      } else {
        window.open(this.urls.baseUrl + url);
      }
    },
    //新增评审相关对话框显示状态
    //显示评审对话框（内部、组织评审）
    showReviewDialog: function() {
      let reviewMethod = this.reviewerExpertTemp.reviewMethod;
      this.selectReviewMethodDialog = false;
      if (reviewMethod == 0) {
        //清空内部评审数据
        this.applyExpertList = [];
        this.applyExpertTotal =  0; //申请专家总条数
        this.applyExpertPage = 1; //申请专家当前页
        this.applyExpertSize = 10; //申请专家每页显示条数
        this.searchAllApplyExpert(this.projectInfo.id);
        this.internalReviewDialogFlag = true;
      } else if (reviewMethod == 1) {
        //清空评审专家筛选
        this.notApplyExpertList = [];
        this.reviewExpertList = [];
        this.notApplyExpertTotal = 0; //非申请专家总条数
        this.notApplyExpertPage = 1; //非申请专家当前页
        this.notApplyExpertSize = 5; //非申请专家每页显示条数
        this.expertSearchType = 'expertName';
        this.expertSearch = '';
        this.searchAllNotApplicationExpert(this.projectInfo.id);
        this.organizationReviewDialogFlag = true;
      }
    },
    //显示组织评审的下一步对话框
    showOrganizationReviewNextDialog: function() {
      if (this.reviewExpertList == null || this.reviewExpertList.length == 0) {
        layer.msg("请选择评审专家！", {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      /*this.organizationReviewDialogFlag = false;*/
      //清空表单数据
      $("input[type='reset']").trigger("click");
      $("#reviewForm").validate().resetForm();
      this.FileUpload = [];
		  this.applyExpertList = [];
      this.applyExpertTotal =  0; //申请专家总条数
      this.applyExpertPage = 1; //申请专家当前页
      this.applyExpertSize = 5; //申请专家每页显示条数
      this.searchAllApplyExpert(this.projectInfo.id);
		  this.organizationReviewNextDialogFlag = true;
    },
    //业务操作
    //删除附件
    deleteEnclosure: function() {
      this.FileUpload = [];
      this.reviewerExpertTemp.standardUrl = "";
      $("input[name='standardUrl']").val("");
      $("#standardUrl-error").css("display","block");
    },
    //查询所有评审专家
    searchAllReviewExpert: function(projectId,matters,reviewNum) {
      let that = this;
		  if (projectId != null && matters != null && reviewNum != null) {
        that.reviewExpertList = [];
		    that.$post(
          that.urls.getAllReviewExpert,
          {"projectId": projectId, "reviewNum": reviewNum, "matters": matters}
        )
          .then(function(res) {
            if(res.success) {
              let reviewExpertList = [];
              //let notApplyExpertList = that.notApplyExpertList;
              let expertList = res.data;
              for (var i = 0,len = expertList.length;i < len;i++) {
                /*for (var j = 0,len2 = notApplyExpertList.length;j < len2;j++) {
                  if (expertList[i].id == notApplyExpertList[j].expertId) {
                    reviewExpertList.push(notApplyExpertList[j]);
                  }
                }*/
                reviewExpertList.push({"expertId": expertList[i].id});
              }
              that.reviewExpertList = reviewExpertList;
              console.log("---"+that.reviewExpertList);
            }
          })
      }
    },
    //查询评审信息
    searchProjectReviewResult: function(projectId) {
      let that = this;
      if (projectId != null) {
        that.projectReviewResultList = [];
        that.projectReviewResultList2 = [];
        //查询第一次评审
        that.$post(
          that.urls.getAllProjectReviewResult,
          {"projectId": projectId, "reviewNum": 1, "matters": 0}
        )
        .then(function(res) {
          if(res.success) {
            that.projectReviewResultList = res.data;
            console.log(that.projectReviewResultList);
          }
        })
        //查询第二次评审
        that.$post(
          that.urls.getAllProjectReviewResult,
          {"projectId": projectId, "reviewNum": 2, "matters": 0}
        )
        .then(function(res) {
          if(res.success) {
            that.projectReviewResultList2 = res.data;
          }
        })
      }
    },
    //查询项目
    searchProject: function(id) {
      let that = this;
		  that.projectInfo = {};
		  if (id != null) {
        that.$post(
          that.urls.getProjectVOReview,
          {"id": id, "reviewMatters": 0}
        )
        .then(function(res) {
          if(res.success) {
            that.projectInfo = res.data;
          }
        })
      }
    },
    //搜索数据列表
    searchTodo: function() {
      let that = this;
      that.$post(
        that.urls.getUnReviewProjectPageInfo,
        {"pageNum": that.page,"pageSize": that.size,"search": that.search}
      )
      .then(function(res) {
        if(res.success) {
          that.projectList = res.pageInfo.list;
          that.total = res.pageInfo.total;
          that.page = res.pageInfo.pageNum;
        }
      })
    },
    //搜索所有非申请专家信息列表
    searchAllNotApplicationExpertTodo: function() {
		  this.searchAllNotApplicationExpert(this.projectInfo.id);
    },
    //搜索所有申请专家未审核信息列表
    searchAllApplyExpert: function(projectId) {
      let that = this;
      that.applyExpertList = [];
      that.$post(
        //that.urls.getALLApplicationExpert,
        that.urls.getApplicationExpertPageInfo,
        {"matters": 0,"projectId": projectId,"pageNum": that.applyExpertPage,"pageSize": that.applyExpertSize}
      )
      .then(function(res) {
        if(res.success) {
          //that.applyExpertList = res.data;
          that.applyExpertList = res.pageInfo.list;
          that.applyExpertTotal = res.pageInfo.total;
          that.applyExpertPage = res.pageInfo.pageNum;
          console.log(that.applyExpertTotal);
        }
      })
    },
    //搜索所有非申请专家信息列表
    searchAllNotApplicationExpert: function(projectId) {
      let that = this;
      that.notApplyExpertLis = [];
      let obj = {};
      obj["projectId"] = projectId;
      obj["pageNum"] = that.notApplyExpertPage;
      obj["pageSize"] = that.notApplyExpertSize;
      obj[that.expertSearchType] = that.expertSearch;
      that.$post(
        that.urls.getNotApplicationExpertPageInfo,
        obj
      )
        .then(function(res) {
          if(res.success) {
            //that.notApplyExpertList = res.data;
            that.notApplyExpertList = res.pageInfo.list;
            that.notApplyExpertTotal = res.pageInfo.total;
            that.notApplyExpertPage = res.pageInfo.pageNum;
          }
        })
    },
    //添加审核专家
    addReviewExpert: function(expert) {
		  let flg = true; //是否是新数据
      let reviewExpertList = this.reviewExpertList;
		  for (let i = 0,len = reviewExpertList.length;i < len;i++) {
		    if (reviewExpertList[i].expertId == expert.expertId) {
          flg = false;
		      break;
        }
      }
		  if (expert != null && flg) {
        this.reviewExpertList.push(expert);
      }
    },
    //删除审核专家
    removeReviewExpert: function(obj) {
      if (obj != null) {
        let reviewExpertList = this.reviewExpertList;
        for (let i = 0,len = reviewExpertList.length;i < len;i++) {
          if (reviewExpertList[i].expertId == obj.expertId) {
            this.reviewExpertList.splice(i,1);
            break;
          }
        }
      }
    },
    //保存评审信息
    saveReviewerExpert: function() {
      let that = this;
      //校验评审表单数据
      let flg = that.reviewValidate.form();
      if (!flg) {
        return;
      }
      let reviewExpertIds = []; //评审专家id集合
      let projectApplicationIds = []; //申请专家id集合
      let reviewExpertList = that.reviewExpertList; //评审专家信息集合
      let applyExpertList = that.applyExpertList; //评审专家信息集合
      //校验评审专家和申请专家不能为空
      if (reviewExpertList == null || applyExpertList == null || reviewExpertList.length == 0 || applyExpertList.length == 0) {
        layer.msg("请选择评审专家和者申请专家", {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      for (let i = 0,len = reviewExpertList.length;i < len;i++) {
        reviewExpertIds.push(reviewExpertList[i].expertId)
      }
      for (let i = 0,len = applyExpertList.length;i < len;i++) {
        projectApplicationIds.push(applyExpertList[i].id)
      }
      let obj = that.reviewerExpertTemp;
      //评审专家id
      obj["reviewExpertIds"] = reviewExpertIds;
      //申请专家id
      obj["projectApplicationIds"] = projectApplicationIds;
      //项目id
      obj["projectId"] = this.projectInfo.id;
      //评审事宜（0：项目申请；1：项目结题）
      obj["matters"] = 0;
      //评审次数
      obj["reviewNum"] = this.projectInfo.reviewNum == null ? 1 : this.projectInfo.reviewNum + 1;
      that.$post(
        that.urls.batchAddProjectReview,
        obj
      )
      .then(function(res) {
        let msg = "";
        if(res.success) {
          msg = '通知审核成功！';
          that.searchTodo();
          that.closeModel();
        } else {
          msg = '通知审核失败！';
        }
        layer.msg(msg, {
          time: 3000, //3s后自动关闭
        });
      })

    },
    //筛选专家
    selectionExpert: function (projectId, expertId) {
		  if (projectId != null && expertId != null) {
        let that = this;
        that.$post(
          that.urls.selectionProjectApplication,
          {"projectId": projectId, "expertId": expertId, "matters": 0}
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = '审核成功！';
            that.searchTodo();//重新渲染列表数据
            that.closeModel();//关闭所有对话框
          } else {
            msg = '审核失败！';
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    }
	}
}
